package astrolabe;

import java.net.*;
import java.io.*;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

import org.jfarrell.classes.Command;

public class CmdServer extends Thread
{
	private ServerSocket server;
	private Socket socket;
	private ObjectInputStream in;
	public Command c;
        private Astrolabe al;
        
	public CmdServer(int port, Astrolabe al)
	{
                this.al = al;
		try
		{
			server = new ServerSocket(port);
			al.vv.Log("CmdServer: Accepting connections..");
			socket = server.accept();
                        al.vv.Log("CmdServer: Accepted connection");
			in = new ObjectInputStream(socket.getInputStream());
                        al.vv.Log("CmdServer: Created InputStream");
                }
		catch(IOException io)
		{
			io.printStackTrace();
			al.vv.Log("CmdServer: ERROR(I/O)");
		}
	}
	
        public void run()
        {
            try
            {
                while(true)
                {
                    
                    Object ob = in.readObject();
                    if(ob.getClass()==Command.class)
                    {
                        c = (Command)ob;// in.readObject();
                        al.cc=c;
                    }
                    /*if(c.getCmd()==BotCommand.PATH)
                    {
                        ImageUpdater iu = new ImageUpdater(c, al);
                        iu.start();
                    }*/
                }
            }catch(IOException io)
            {
                io.printStackTrace();
                System.out.println("CMDSERVER: I/O Error in Server.Server()");
            }
            catch(ClassNotFoundException cnf)
            {
                cnf.printStackTrace();
                System.out.println("CMDSERVER: Error of some sort.  Probably with in.readObject");
            }
        }
	
        
        
	/*
	public static void main(String[] args)
	{
		if (args.length != 1) {
			System.out.println( "Syntax Error: java Server <listening port>" );
			System.exit( 0 );
		}
		new Server( Integer.parseInt(args[0]) );
	}*/
}

